const express = require('express');
const router = express.Router();
const connection = require('../connection');

router.post('/add', (req, res, next) => {
    let obj = req.body;
    console.log(obj);
    connection.query('insert into dept set ?', [obj], (err, r) => {
        if (err) { 
            return next(err);
        }  
        res.send({
            code:200,
            msg:r
        })
    })
});

router.get('/detail',(req,res,next)=>{
    let obj = req.query;
    console.log(obj);
    let sql = 'select * from dept where did = ?';
    connection.query(sql,[obj.did],(err,r)=>{
        if(err){
            return next(r);
        }
        if(r.length===0){
            res.send({
                code:400,
                msg:'找不到id',
                date:r
            })
        }else{
            res.send({
                code:200,
                msg:'查询成功',
                date:r
            })
        }
        
    })
});
router.get('/details',(req,res,next)=>{
    let obj = req.query;
    console.log(obj);
    let sql = 'select * from dept';
    connection.query(sql,[],(err,r)=>{
        if(err){
            return next(r);
        }
        if(r.length===0){
            res.send({
                code:400,
                msg:'找不到id',
                date:r
            })
        }else{
            res.send({
                code:200,
                msg:'查询成功',
                date:r
            })
        }
        
    })
});
router.delete('/del',(req,res,next)=>{
    let obj = req.query;
    let sql = 'delete from dept where did = ?';
    connection.query(sql,[obj.did],(err,r)=>{
        if(err){
            return next(r);
        }
        console.log(r);
        if(r.affectedRow>0){
            res.send({
                code:200,
                msg:'删除成功',
                date:r
            })
        }else{
            res.send({
                code:400,
                msg:'没有这个id',
                date:r
            })
        }
        
    })
});
router.put('/upd', (req, res, next) => {
    // res.setHeader('Content-Type', 'application/json;charset=utf-8');
    // 获取get传递的参数
    var obj = req.body;
    console.log(obj)
    // 执行SQL命令
    connection.query('update dept set ? where did=?', [obj, obj.did], (err, r) => {
        if (err) {
            return next(err);
        }
        if (r.affectedRows === 0) {
            res.send({
                code: 400,
                msg: '找不到此编号的员工，修改失败'
            })
        } else {
            res.send({
                code: 200,
                msg: '修改成功'
            })
        }
    })

});

//分页查询
router.get('/list',(req,res,next)=>{
    let obj = req.query;
    if(!obj.pno){
        obj.pno=1;
    };
    if(!obj.count){
        obj.count=5;
    };
    let start = (obj.pno - 1) * obj.count;
    let size = parseInt(obj.count);
    let sql = 'select * from dept order by did desc limit ?,?';
    let sql1 = 'select count(*) as count from dept';
    connection.query(`${sql};${sql1}`,[start,size],(err,r)=>{
        if(err){
            return next(err);
        }
        console.log(r);
        res.send({
            code:200,
            index:obj.pno,
            count:r[1],
            msg:'部门列表',
            data:r[0]
        })
    });
})
module.exports = router;